-- Tags: no-fasttest, long, no-debug, no-tsan, no-asan, no-msan, no-ubsan

SET enable_json_type = 1;

drop table if exists test;

{% for create_command in ['create table test (id UInt64, json JSON(max_dynamic_paths=3)) engine=MergeTree order by id settings min_rows_for_wide_part=1000000000, min_bytes_for_wide_part=10000000000, vertical_merge_algorithm_min_columns_to_activate=1;',
                         'create table test (id UInt64, json JSON(max_dynamic_paths=3)) engine=MergeTree order by id settings min_rows_for_wide_part=1, min_bytes_for_wide_part=1, vertical_merge_algorithm_min_columns_to_activate=1;'] -%}

select '{{ create_command }}';

{{ create_command }}

system stop merges test;
insert into test select number, toJSONString(map('a', number)) from numbers(100000);
insert into test select number, toJSONString(map('b', number)) from numbers(90000);
insert into test select number, toJSONString(map('c', number)) from numbers(80000);
insert into test select number, toJSONString(map('d', number)) from numbers(70000);
insert into test select number, toJSONString(map('e', number)) from numbers(60000);
insert into test select number, '{}' from numbers(100000);

select 'Dynamic paths';
select count(), arrayJoin(JSONDynamicPaths(json)) as path from test group by path order by count() desc, path;
select 'Shared data paths';
select count(), arrayJoin(JSONSharedDataPaths(json)) as path from test group by path order by count() desc, path;
system start merges test;
optimize table test final;
select 'Dynamic paths';
select count(), arrayJoin(JSONDynamicPaths(json)) as path from test group by path order by count() desc, path;
select 'Shared data paths';
select count(), arrayJoin(JSONSharedDataPaths(json)) as path from test group by path order by count() desc, path;

system stop merges test;
insert into test select number, toJSONString(map('f', number)) from numbers(200000);
select 'Dynamic paths';
select count(), arrayJoin(JSONDynamicPaths(json)) as path from test group by path order by count() desc, path;
select 'Shared data paths';
select count(), arrayJoin(JSONSharedDataPaths(json)) as path from test group by path order by count() desc, path;
system start merges test;
optimize table test final;
select 'Dynamic paths';
select count(), arrayJoin(JSONDynamicPaths(json)) as path from test group by path order by count() desc, path;
select 'Shared data paths';
select count(), arrayJoin(JSONSharedDataPaths(json)) as path from test group by path order by count() desc, path;

system stop merges test;
insert into test select number, toJSONString(map('g', number)) from numbers(10000);
select 'Dynamic paths';
select count(), arrayJoin(JSONDynamicPaths(json)) as path from test group by path order by count() desc, path;
select 'Shared data paths';
select count(), arrayJoin(JSONSharedDataPaths(json)) as path from test group by path order by count() desc, path;
system start merges test;
optimize table test final;
select 'Dynamic paths';
select count(), arrayJoin(JSONDynamicPaths(json)) as path from test group by path order by count() desc, path;
select 'Shared data paths';
select count(), arrayJoin(JSONSharedDataPaths(json)) as path from test group by path order by count() desc, path;

system stop merges test;
insert into test select number, toJSONString(map('c', number)) from numbers(40000);
select 'Dynamic paths';
select count(), arrayJoin(JSONDynamicPaths(json)) as path from test group by path order by count() desc, path;
select 'Shared data paths';
select count(), arrayJoin(JSONSharedDataPaths(json)) as path from test group by path order by count() desc, path;
system start merges test;
optimize table test final;
select 'Dynamic paths';
select count(), arrayJoin(JSONDynamicPaths(json)) as path from test group by path order by count() desc, path;
select 'Shared data paths';
select count(), arrayJoin(JSONSharedDataPaths(json)) as path from test group by path order by count() desc, path;

drop table test;

{% endfor -%}
